﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using mana.Web.UI;
using System.IO;

namespace TestWebSite.UI
{
    public partial class PageBasePage : PageBase
    {
        const string TEXT_KEY = "Text";
        protected string Text
        {
            get { return GetFromViewState<string>(TEXT_KEY); }
            set { SetToViewState<string>(TEXT_KEY, value); }
        }

        const string TEXT2_KEY = "Text2";
        protected string Text2
        {
            get { return GetFromSession<string>(TEXT2_KEY); }
            set { SetToSession<string>(TEXT2_KEY, value); }
        }

        const string DATETIME_KEY = "DateTime";
        protected DateTime DateTime
        {
            get { return GetFromViewState<DateTime>(DATETIME_KEY); }
            set { SetToViewState<DateTime>(DATETIME_KEY, value); }
        }

        const string DATETIME2_KEY = "DateTime2";
        protected DateTime DateTime2
        {
            get { return GetFromSession<DateTime>(DATETIME2_KEY); }
            set { SetToSession<DateTime>(DATETIME2_KEY, value); }
        }

        const string TESTCLASS_KEY = "TestClass";
        protected TestClass TestClass
        {
            get { return GetFromViewState<TestClass>(TESTCLASS_KEY); }
            set { SetToViewState<TestClass>(TESTCLASS_KEY, value); }
        }

        const string TESTCLASS2_KEY = "TestClass2";
        protected TestClass TestClass2
        {
            get { return GetFromSession<TestClass>(TESTCLASS2_KEY); }
            set { SetToSession<TestClass>(TESTCLASS2_KEY, value); }
        }

        const string LONG_KEY = "Long";
        public long Long 
        {
            get { return GetFromViewState<long>(LONG_KEY); }
            set { SetToViewState<long>(LONG_KEY, value); }
        }

        const string LONG2_KEY = "Long2";
        public long Long2
        {
            get { return GetFromSession<long>(LONG2_KEY); }
            set { SetToSession<long>(LONG2_KEY, value); }
        }

        const string LIST_TESTCLASS_KEY = "ListTestClass";
        public List<TestClass> ListTestClass
        {
            get { return GetFromViewState<List<TestClass>>(LIST_TESTCLASS_KEY); }
            set { SetToViewState<List<TestClass>>(LIST_TESTCLASS_KEY, value); }
        }

        const string LIST2_TESTCLASS_KEY = "List2TestClass";
        public List<TestClass> ListTestClass2
        {
            get { return GetFromSession<List<TestClass>>(LIST2_TESTCLASS_KEY); }
            set { SetToSession<List<TestClass>>(LIST2_TESTCLASS_KEY, value); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                var text = Text;
                var dateTime = DateTime;
                var testClass = TestClass;
                var oLong = Long;
                var list = ListTestClass;

                var text2 = Text2;
                var dateTime2 = DateTime2;
                var testClass2 = TestClass2;
                var oLong2 = Long2;
                var list2 = ListTestClass2;
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnSetViewStateValues_Click(object sender, EventArgs e)
        {
            try
            {
                var now = DateTime.Now;

                Text = string.Format("Some text {0}", now.Millisecond);
                DateTime = now.AddMinutes(now.Millisecond);
                TestClass = new TestClass 
                    { 
                        Id = now.Hour + now.Minute + now.Second + now.Millisecond + now.Day + now.Month + now.Year, 
                        Text = string.Format("My text {0}", now.Millisecond), 
                        Boolean = (now.Minute % 2) == 0 
                    };
                Long = now.Ticks;
                
                if (ListTestClass == null)
                    ListTestClass = new List<TestClass>();
                ListTestClass.Add(TestClass);
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnSetSessionValues_Click(object sender, EventArgs e)
        {
            try
            {
                var now = DateTime.Now;

                Text2 = string.Format("Some text {0}", now.Millisecond);
                DateTime2 = now.AddMinutes(now.Millisecond);
                TestClass2 = new TestClass
                {
                    Id = now.Hour + now.Minute + now.Second + now.Millisecond + now.Day + now.Month + now.Year,
                    Text = string.Format("My text {0}", now.Millisecond),
                    Boolean = (now.Minute % 2) == 0
                };
                Long2 = now.Ticks;

                if (ListTestClass2 == null)
                    ListTestClass2 = new List<TestClass>();
                ListTestClass2.Add(TestClass2);
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnAuthenticatedUser_Click(object sender, EventArgs e)
        {
            try
            {
                SetAuthenticatedUserCookieAndRedirectToDefaultPage(txtUserName.Text);
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnRedirectToLoginPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(txtRedirectToLoginPage.Text))
                    RedirectsToLoginPage(txtRedirectToLoginPage.Text);
                else
                    RedirectsToLoginPage();
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnShowMessage_Click(object sender, EventArgs e)
        {
            try
            {
                ShowMessage(lblMessageToShow, txtMessageToShow.Text, "mensaje-info");
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnClearMessage_Click(object sender, EventArgs e)
        {
            try
            {
                ClearMessage(lblMessageToShow);
            }
            catch (Exception)
            {
                //
            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        {
            try
            {
                ShowFileDownloadDialog(fupArchivo.FileName, fupArchivo.FileBytes);
            }
            catch (Exception)
            {
                //
            }
        }
    }

    [Serializable]
    public class TestClass
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public bool Boolean { get; set; }
    }
}